/**
 * 
 */
package unifor.filetransfer.transmissor;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author Landim - Arthur Landim
 *
 */
public class EnviarArquivo {
    
    public static void main(String[] args) {
//        try {
//            new EnviarArquivo().read2array("E:\\install\\java\\eclipse\\wtp-all-in-one-sdk-R-1.5.0-200606281455-win32.zip");
//        } catch (Exception e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
        
//        System.out.println("E:/install/mysql-4.1.20-win32.zip".replaceAll(".*\\/", ""));
    	
    	File libDir = new File("/Users/landim/Trabalhos/java/FileTransfer/lib");
//    	System.out.print("java -cp \"");
    	if (libDir.isDirectory()) {
    		File[] files = libDir.listFiles();
    		for(int i=1;i<files.length;i++)
    		System.out.print((i != 1 ? "," : "") +"lib/"+files[i].getName());
    	}
    	System.out.println(",bin/,gui/\" ");// + unifor.filetransfer.Principal.class.getName() + " context.xml");
    	
    }
    
    /**
     *  Reads a file storing intermediate data into an array.
     *  @param file the file to be read
     *  @return a file data
     */
     public byte[] read2array(String file) throws Exception {
         InputStream in = null;
         OutputStream outStream = null;
//        byte[] out             = new byte[0];

        try{
            in = new BufferedInputStream(new FileInputStream(file));
            outStream = new BufferedOutputStream(new FileOutputStream("E:\\landim\\wtp-all-in-one-sdk-R-1.5.0-200606281455-win32.zip"));

           // the length of a buffer can vary
           int bufLen = 2000*1024;
           
           byte[] buf = new byte[bufLen];
           byte[] tmp = null;
           int len    = 0;
           int index = 0;
           
           while((len = in.read(buf,0,bufLen)) != -1){
              // extend array
//               tmp = new byte[out.length + len];
               tmp = new byte[len];

              // copy data
//              System.arraycopy(out,0,tmp,0,out.length);
//              System.arraycopy(buf,0,tmp,out.length,len);
//              out = tmp;
               System.out.println("copiando pacote " + index++);
               if (len < bufLen) {
                   tmp = new byte[len];
                   System.arraycopy(buf,0,tmp,0,len);
                   outStream.write(tmp);
               } else {
                   outStream.write(buf);
               }
               outStream.flush();
              tmp = null;        
              buf = new byte[bufLen];

           }

        } finally {
           // always close the stream
           if (in != null) {
               try{ 
                   in.close();
                   outStream.close();
               }catch (Exception e){
                   
               }
           }

        }
        return null;
     }
}
